package com.wwg.threads.threads_interactive.volatile_test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @类描述: TODO
 * @Author wwg
 * @date: 2021/7/28 18:28
 * @Version: 1.0
 **/
public class VolatileTest {

    private static final Logger log = LoggerFactory.getLogger(VolatileTest.class);
    private volatile static boolean flag;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    if (flag){
                        log.debug("flag 标识被改成了true!");
                        break;
                    }
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    log.debug("第{}次循环",i+1);
                    if (i==5) {
                        flag = true;
                    }
                }
            }
        });

        t1.start();
        Thread.sleep(1000);
        t2.start();

    }
}
